package com.malamute.system.action.system;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.cafe.model.Gender;
import com.malamute.core.action.BaseAction;
import com.malamute.core.action.CrudMethod;
import com.malamute.core.common.SearchParamUtil;
import com.malamute.system.entity.User;
import com.malamute.system.service.UserService;

/**
 * @Title:UserAction.java
 * @Package com.cafe.action
 * @Description 会员Action模块
 * @author AceGuan gjybst@163.com
 * @date 2012-1-28 下午02:32:55
 * @version v1.0
 */

/**
 * 跳转分为两类： 1.链接地址跳转 2.Action跳转 type = "redirect"为直接跳转页面 type = "redirectAction"
 * 跳转Action
 */
// @Results({
// @Result(name = BaseAction.LIST, location = "user.jsp"),// 查询时跳转到列表页面
// @Result(name = BaseAction.FORM, location = "user_form.jsp"),// 查询时跳转到列表页面
// @Result(name = BaseAction.ACTION, type = "redirectAction", params = {
// "actionName", "user!list" }) // 保存成功后列表Action 跳转
// })
public class UserAction extends BaseAction implements CrudMethod {
	private User user;
	@Autowired(required = false)
	private UserService userService;
	
	private Map<String, String> genderMap = new HashMap<String, String>();

	@Override
	public void prepare() throws Exception {
		for (Gender g : Gender.values()) {
			genderMap.put(g.getKey(), g.getValue());
		}
	}

	/**
	 * 获取user
	 * 
	 * @return user
	 */

	public User getUser() {
		return user;
	}

	/**
	 * 设置user
	 * 
	 * @param user
	 */
	public void setUser(User user) {
		this.user = user;
	}

	public String list() {
		DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
		criteria.add(Restrictions.ne("username", "admin") );
		SearchParamUtil.simpleSeach(criteria, searchParam);
		userService.listUsersByPage(criteria, page);
		return LIST;
	}
	
	@Override
	public String form() throws Exception {
		if (user != null) {
			setUser(userService.getUserById(user.getId()));
		}
		return FORM;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#save()
	 */
	@Override
	public String save() throws Exception {
		userService.saveUser(user);
		return ACTION;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#update()
	 */
	@Override
	public String update() throws Exception {
		userService.updateUser(user);
		return ACTION;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#search()
	 */
	@Override
	public String search() throws Exception {
		return this.list();
	}

	public Map<String, String> getGenderMap() {
		return genderMap;
	}

	public void setGenderMap(Map<String, String> genderMap) {
		this.genderMap = genderMap;
	}

}
